package navigation {
	import util.TextFieldFactory;
	import org.splink.resource.ResourceProvider;

	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.EventDispatcher;
	import flash.events.MouseEvent;
	import flash.text.TextField;

	/**
	 * @author Max Kugland
	 */
	public class Btn extends EventDispatcher {
		private var _display : Sprite;
		private var _id : String;
		private var _text : String;
		private var _marked : Boolean;
		private var _tf : TextField;

		public function Btn(id : String, text : String) {
			_id = id;
			_text = text;
			_display = new Sprite();
			_display.mouseChildren = false;			_display.buttonMode = true;
			
			_display.addChild(_tf = TextFieldFactory.singleline(_text, 12, ResourceProvider.instance().getFontDataByType("default")));			
			addListeners();
			unmark();
		}

		private function addListeners() : void {
			_display.addEventListener(MouseEvent.CLICK, onClick, false, 0, true);
			_display.addEventListener(MouseEvent.ROLL_OVER, onRollover, false, 0, true);
			_display.addEventListener(MouseEvent.ROLL_OUT, onRollout, false, 0, true);
		}

		private function onRollout(event : MouseEvent = null) : void {
			if(!_marked) {
				_tf.htmlText = _text;
			}
		}

		private function onRollover(event : MouseEvent = null) : void {
			if(!_marked) {
				_tf.htmlText = "<b>" + _text + "</b>";
			}
		}

		public function mark() : void {
			onRollover();
			_marked = true;
		}

		public function unmark() : void {
			_marked = false;
			onRollout();
		}

		private function onClick(event : MouseEvent) : void {
			dispatchEvent(event.clone());
		}

		public function get display() : DisplayObject {
			return _display;
		}

		public function get id() : String {
			return _id;
		}
	}
}
